package org.hit.burkun.umls.relation;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hit.burkun.db.DBHelper;
import org.hit.burkun.entities.Entity;
import org.hit.burkun.umls.GeneralQuery;
import org.hit.burkun.umls.relation.UniformRelation.TYPE;

public class ClinvarRelationAdpater {
	// disease-gene, 各种evidence
	public static void main(String[] args) {

		Collection<UniformRelation> res1 = getClinvar2GeneRel();
		UniformRelationDump.dumpRelationsWithoutPrimaryId(res1, false,
				"birelation");
	}

	public static Collection<UniformRelation> getClinvar2GeneRel() {
		// query do 2 gene
		DBHelper dbh = DBHelper.getServerBioSearch();
		dbh.connectionDB();
		Collection<String[]> rawRes = dbh.queryData(
				"select umls_cuis, gene_id from clinvar_umls_geneid", 2);
		dbh.disconnection();
		HashSet<String> terms2 = new HashSet<String>();
		for (String[] line : rawRes) {
			terms2.add(line[1]);
		}
		// not update unmpapped
		GeneralQuery gq = new GeneralQuery(DBHelper.getServerData(),
				"map_umls_ids", false);
		gq.begin();
		gq.quickMap(terms2, Entity.TYPE.GENE_ID);
		gq.end();
		Map<String, Set<String>> mapRes = gq.getResult();
		Collection<UniformRelation> res = new LinkedList<UniformRelation>();

		for (String[] line : rawRes) {
			if (mapRes.containsKey(line[1])) {
				String umls1 = line[0];
				// filter
				boolean isContain = OntologySiddUmlsFilter.isContain(umls1);
				if (isContain) {
					String u1 = umls1;
					Set<String> umls2 = mapRes.get(line[1]);
					String u2 = StringUtils.join(umls2, ",");
					Entity ent1 = new Entity(null, null, u1, line[0],
							Entity.TYPE.DISEASE);
					Entity ent2 = new Entity(null, null, u2, line[1],
							Entity.TYPE.GENE_ID);
					UniformRelation ur = new UniformRelation(ent1, ent2, false,
							TYPE.DISEASE_CLINVAR_GENE);
					//clinvar 
					res.add(ur);
				}
			}
		}
		return res;
	}

}
